//
//  ViewController.m
//  ZWLMusic
//
//  Created by dllo on 16/1/6.
//  Copyright © 2016年 ZKJ. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UISegmentedControl *segment;
@property (nonatomic, strong) UIView *bottomView;

@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self createSegment];
    
    [self createCollectionView];
    
}

// 创建segment
- (void)createSegment {
    self.segment = [[UISegmentedControl alloc] initWithItems:@[@"推荐", @"歌单", @"MV", @"排行榜"]];
    [self.view addSubview:self.segment];
    [self.view bringSubviewToFront:self.segment];
    [self.segment addTarget:self action:@selector(segmentValueChanged:) forControlEvents:UIControlEventValueChanged];
    
    self.segment.tintColor = [UIColor clearColor];
    self.segment.frame = CGRectMake(0, 0, self.view.width, 30 * ptsHEIGTH);
    self.segment.selectedSegmentIndex = 0;
    
    NSDictionary *selectedText = @{NSFontAttributeName:[UIFont systemFontOfSize:18 * ptsHEIGTH], NSForegroundColorAttributeName:kThemeColor};
    NSDictionary *unSelctedText = @{NSFontAttributeName:[UIFont systemFontOfSize:16 * ptsHEIGTH], NSForegroundColorAttributeName:[UIColor blackColor]};
    [self.segment setTitleTextAttributes:selectedText forState:UIControlStateSelected];
    [self.segment setTitleTextAttributes:unSelctedText forState:UIControlStateNormal];
    
    self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.segment.bottom, self.view.width / 4, 3 * ptsHEIGTH)];
    self.bottomView.backgroundColor = kThemeColor;
    [self.segment addSubview:self.bottomView];
    
}

// segment方法
- (void)segmentValueChanged:(UISegmentedControl *)segment {
    NSInteger index = segment.selectedSegmentIndex;
    // segment下方色块移动动画
    [UIView animateWithDuration:0.2 animations:^{
        self.bottomView.frame = CGRectMake(index * self.view.width / 4, self.bottomView.top, self.bottomView.width, self.bottomView.height);
    } completion:^(BOOL finished) {
        
    }];
    
}

// 创建collectionView
- (void)createCollectionView {
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(self.view.width, self.view.height - 64 - 49 - 33 * ptsHEIGTH);
    flowLayout.minimumInteritemSpacing = CGFLOAT_MIN;
    flowLayout.minimumLineSpacing = CGFLOAT_MIN;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 33 * ptsHEIGTH, self.view.width, self.view.height - 64 - 49 - 33 * ptsHEIGTH) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.contentOffset = CGPointMake(self.view.width, 0);
    self.collectionView.pagingEnabled = YES;
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCellIdentifier"];
    
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCellIdentifier" forIndexPath:indexPath];
    cell.backgroundColor = kThemeColor;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
